//package com.liqi.frame.comm.config;
//
//import com.liqi.frame.comm.properties.AppProperties;
//import org.springframework.amqp.core.*;
//import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.core.RabbitAdmin;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.Objects;
//
///**
// * RabbitMQ的配置
// * Created by liqi on 2019/8/2 0002.
// */
//
//
//@ConditionalOnClass({RabbitTemplate.class})
//@Configuration
//public class RabbitMQConfig {
//    @Autowired
//    private AppProperties appProperties;
//
//    @Bean
//    public ConnectionFactory rmqConnectionFactory() {
//        CachingConnectionFactory connectionFactory = null;
//        String rabbitmqEnable = appProperties.getRabbitMQProperties().getEnable();
//        if (Objects.equals("on", rabbitmqEnable)) {
//            connectionFactory = new CachingConnectionFactory();
//            connectionFactory.setHost(appProperties.getRabbitMQProperties().getHost());
//            connectionFactory.setPort(Integer.parseInt(appProperties.getRabbitMQProperties().getPort()));
//            connectionFactory.setUsername(appProperties.getRabbitMQProperties().getUsername());
//            connectionFactory.setPassword(appProperties.getRabbitMQProperties().getPassword());
//            connectionFactory.setVirtualHost(appProperties.getRabbitMQProperties().getVirtualHost());
//        }
//        return connectionFactory;
//    }
//
//    @Bean
//    public RabbitAdmin rabbitAdmin(ConnectionFactory rmqConnectionFactory){
//        RabbitAdmin rabbitAdmin = null;
//        String rabbitmqEnable = appProperties.getRabbitMQProperties().getEnable();
//        if (Objects.equals("on", rabbitmqEnable)) {
//            rabbitAdmin = new RabbitAdmin(rmqConnectionFactory);
//            // spring容器启动时加载RabbitAdmin
//            rabbitAdmin.setAutoStartup(true);
//        }
//        return rabbitAdmin;
//    }
//
//    @Bean
//    Jackson2JsonMessageConverter messageConverter() {
//        return new Jackson2JsonMessageConverter();
//    }
//
//    @Bean
//    public TopicExchange exchange01(){
//        return new TopicExchange(appProperties.getRabbitMQProperties().getExchangeName(),false,false);
//    }
//    @Bean
//    public TopicExchange exchange02(){
//        return new TopicExchange("topic01",false,false);
//    }
//    @Bean
//    public FanoutExchange exchange03(){
//        return new FanoutExchange("fanout01",false,false);
//    }
//    @Bean
//    public Queue queue01(){
//        //false 是否持久
//        return new Queue("queue01", false);
//    }
//    @Bean
//    public Queue queue02(){
//        return new Queue("queue02", false);
//    }
////    @Bean
////    public Queue queue03(){
////        return new Queue("queue03", false);
////    }
//    @Bean
//    public Binding binding01(){
//        return BindingBuilder.bind(queue01()).to(exchange01()).with("topic.*");
//    }
////    @Bean
////    public Binding binding02(){
////        return BindingBuilder.bind(queue02()).to(exchange01()).with("*.amqp");
////    }
//    @Bean
//    public Binding binding03(){
//        return BindingBuilder.bind(queue02()).to(exchange03());
//    }
////    @Bean
////    public Binding binding04(){
////        return BindingBuilder.bind(queue03()).to(exchange03());
////    }
//
//    @Bean
//    public RabbitTemplate rabbitTemplate(ConnectionFactory rmqConnectionFactory){
//        RabbitTemplate rabbitTemplate = null;
//        String rabbitmqEnable = appProperties.getRabbitMQProperties().getEnable();
//        if (Objects.equals("on", rabbitmqEnable)) {
//            rabbitTemplate = new RabbitTemplate(rmqConnectionFactory);
//        }
//        return rabbitTemplate;
//    }
//
//    /**
//     * 消息监听
//     * @param rmqConnectionFactory
//     * @return
//     */
////    @Bean
////    public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory rmqConnectionFactory){
////        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(rmqConnectionFactory);
//////        当前消费者数量
////        container.setConcurrentConsumers(1);
//////        最大的消费者数量
////        container.setMaxConcurrentConsumers(5);
//////        设置监听的队列
////        container.setQueues(queue02());
//////        是否重回队列
////        container.setDefaultRequeueRejected(false);
//////        签收模式
////        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
//////        标签
////        container.setConsumerTagStrategy(new ConsumerTagStrategy() {
////            @Override
////            public String createConsumerTag(String s) {
////                return "_"+ UUID.randomUUID();
////            }
////        });
////        container.setMessageListener(new ChannelAwareMessageListener() {
////            @Override
////            public void onMessage(Message message, Channel channel) throws Exception {
//////                进行消息处理
////                String msg = new String(message.getBody());
////            }
////        });
////        return container;
////    }
//
//    /**
//     * 消息监听适配器
//     * @param rmqConnectionFactory
//     * @return
//     */
////    @Bean
////    public SimpleMessageListenerContainer messageListenerAdapter(ConnectionFactory rmqConnectionFactory) {
////        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(rmqConnectionFactory);
//////        当前消费者数量
////        container.setConcurrentConsumers(1);
//////        最大的消费者数量
////        container.setMaxConcurrentConsumers(5);
//////        设置监听的队列
////        container.setQueues(queue02());
//////        是否重回队列
////        container.setDefaultRequeueRejected(false);
//////        签收模式
////        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
////        container.setConsumerTagStrategy(new ConsumerTagStrategy() {
////            public String createConsumerTag(String s) {
////                return "_"+ UUID.randomUUID();
////            }
////        });
////        MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageDelegate(),"");
////        adapter.setDefaultListenerMethod("handleMessageText");
////
////        Map<String, String> queueOrTagToMethodName = new HashMap<>();
////        queueOrTagToMethodName.put("queue01", "handleMessageText");
////        adapter.setQueueOrTagToMethodName(queueOrTagToMethodName);
//////        消息转换器
////        adapter.setMessageConverter(new TextMessageConverter());
////
////        container.setMessageListener(adapter);
////
////        return container;
////    }
////
////    @Bean
////    public SimpleMessageListenerContainer messageListenerAdapter2(ConnectionFactory rmqConnectionFactory) {
////        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(rmqConnectionFactory);
//////        当前消费者数量
////        container.setConcurrentConsumers(1);
//////        最大的消费者数量
////        container.setMaxConcurrentConsumers(5);
//////        设置监听的队列
////        container.setQueues(queue03());
//////        是否重回队列
////        container.setDefaultRequeueRejected(false);
//////        签收模式
////        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
////        container.setConsumerTagStrategy(new ConsumerTagStrategy() {
////            public String createConsumerTag(String s) {
////                return "_"+ UUID.randomUUID();
////            }
////        });
////        MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageDelegate());
////        adapter.setDefaultListenerMethod("handleMessage1");
////
////        Map<String, String> queueOrTagToMethodName = new HashMap<>();
////        queueOrTagToMethodName.put("queue02", "handleMessage1");
////        adapter.setQueueOrTagToMethodName(queueOrTagToMethodName);
//////        消息转换器
////        adapter.setMessageConverter(new TextMessageConverter());
////        container.setMessageListener(adapter);
////
////        return container;
////    }
//}
